package com.yunhe.sms.controller.system;

import com.yunhe.commons.dto.Pageable;
import com.yunhe.sms.entity.system.Permission;
import com.yunhe.sms.service.system.PermissionService;
import com.yunhe.sms.service.system.impl.PermissionServiceImpl;
import com.yunhe.webmvc.annotation.RequestBean;
import com.yunhe.webmvc.annotation.RequestMapping;
import com.yunhe.webmvc.annotation.ResponseJson;
import com.yunhe.webmvc.model.JSONResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/10/19 0019.
 */
@RequestMapping("/permission")
public class PermissionController {
    PermissionService permissionService = new PermissionServiceImpl();

    /**
     * 列表页
     * @return
     */
       @RequestMapping
        public String list(){
            return "system/permission/list";
        }

    @RequestMapping("/listData")
    @ResponseJson
    public Pageable list(int offset, int limit){
        Pageable<Permission> pageable = permissionService.findPage(offset, limit);
        return pageable;
    }

    @RequestMapping("/list/child")
    @ResponseJson
    public List<Permission> listByParent(Long parentId){
        List<Permission> listByParent = permissionService.findListByParent(parentId);
        return listByParent;
    }
    /**
     * 添加页
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "system/permission/add";
    }

    /**
     * 编辑页
     * @param id
     * @param req
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long id, HttpServletRequest req){
        Permission permission = permissionService.find(id);

        if(null != permission && permission.getType() > 1) {

            List<Permission> parentList = permissionService.findListByType(permission.getType() == 2 ? 1 : 2);
            req.setAttribute("parentList", parentList);
        }

        req.setAttribute("permission",permission);
        return "system/permission/edit";
    }

    /**
     * 添加操作
     * @param permission
     * @return
     */
    @RequestMapping("/save")
    public String save(@RequestBean Permission permission){
        permissionService.save(permission);
        return "redirect:/permission";
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    /*@RequestMapping("/delete")
    public String delete(Long id){
        permissionService.delete(id);
        return "redirect:/permission/list";
    }
*/

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @ResponseJson
    public JSONResponse deleteBatch(String ids){
        permissionService.deleteBatch(ids);
        return JSONResponse.success("删除成功");
    }

    /**
     * 修改操作
     * @param permission
     * @return
     */
    @RequestMapping("/update")
    public String update(@RequestBean Permission permission){
            permissionService.update(permission);
            return "redirect:/permission";
    }


    @RequestMapping("/type")
    @ResponseJson
    public JSONResponse findListByType(Integer type){
            List<Permission> listByType = permissionService.findListByType(type);
            return JSONResponse.success("成功",listByType);
    }
}
